// 缓存最多页数
const MAX_CACHE_PAGEINDEX = 3;
// 缓存页签数量
const MAX_CACHE_PAGE = 3;
const newsData = [{
    title: '温故知新老师们这些话要牢记',
    imgArr: ['/static/images/product/2.jpg', '/static/images/product/3.jpg', '/static/images/product/4.jpg'],
    badgeType: 1,
    badgeText: '要闻',
    subContent: '央视网新闻'
  },
  {
    title: '美国会表决通过新驻华大使 月底有望赴华',
    imgArr: [],
    badgeType: 2,
    badgeText: '要闻',
    subContent: '央视网新闻'
  },
  {
    title: '哪些专业毕业后收入高?计算机、金融专业排前列金融专业排前列金融专业排前列金融专业排前列金融专业排前列',
    imgArr: ['/static/images/product/2.jpg', '/static/images/product/3.jpg', '/static/images/product/4.jpg'],
    badgeType: 3,
    badgeText: '本地资讯',
    subContent: '粤港精英联盟'
  },
  {
    title: '科创板交易系统准备就绪,不存在首批名单',
    imgArr: [],
    badgeType: 4,
    badgeText: '朋友都看过',
    subContent: '百科故事大全'
  },
  {
    title: '哪些专业毕业后收入高?计算机、金融专业排前列金融专业排前列金融专业排前列金融专业排前列金融专业排前列',
    imgArr: ['/static/images/product/2.jpg', '/static/images/product/3.jpg', '/static/images/product/4.jpg'],
    badgeType: 1,
    badgeText: '本地资讯',
    subContent: '粤港精英联盟'
  },
  {
    title: '温故知新老师们这些话要牢记',
    imgArr: ['/static/images/product/2.jpg', '/static/images/product/3.jpg', '/static/images/product/4.jpg'],
    badgeType: 1,
    badgeText: '要闻',
    subContent: '央视网新闻'
  },
  {
    title: '温故知新老师们这些话要牢记',
    imgArr: ['/static/images/product/2.jpg', '/static/images/product/3.jpg', '/static/images/product/4.jpg'],
    badgeType: 1,
    badgeText: '要闻',
    subContent: '央视网新闻'
  },
  {
    title: '美国会表决通过新驻华大使 月底有望赴华',
    imgArr: [],
    badgeType: 2,
    badgeText: '要闻',
    subContent: '央视网新闻'
  },
  {
    title: '哪些专业毕业后收入高?计算机、金融专业排前列金融专业排前列金融专业排前列金融专业排前列金融专业排前列',
    imgArr: ['/static/images/product/2.jpg', '/static/images/product/3.jpg', '/static/images/product/4.jpg'],
    badgeType: 3,
    badgeText: '本地资讯',
    subContent: '粤港精英联盟'
  },
  {
    title: '科创板交易系统准备就绪,不存在首批名单',
    imgArr: [],
    badgeType: 4,
    badgeText: '朋友都看过',
    subContent: '百科故事大全'
  },
  {
    title: '哪些专业毕业后收入高?计算机、金融专业排前列金融专业排前列金融专业排前列金融专业排前列金融专业排前列',
    imgArr: ['/static/images/product/2.jpg', '/static/images/product/3.jpg', '/static/images/product/4.jpg'],
    badgeType: 1,
    badgeText: '本地资讯',
    subContent: '粤港精英联盟'
  },
  {
    title: '温故知新老师们这些话要牢记',
    imgArr: ['/static/images/product/2.jpg', '/static/images/product/3.jpg', '/static/images/product/4.jpg'],
    badgeType: 1,
    badgeText: '要闻',
    subContent: '央视网新闻'
  }
];
Page({
  data: {
    newsList: [],
    cacheTab: [],
    tabIndex: 0,
    tabBars: [{
        name: '热门',
        id: 'hot'
      },
      {
        name: '娱乐',
        id: 'yule'
      },
      {
        name: '体育',
        id: 'sports'
      },
      {
        name: '国内',
        id: 'domestic'
      },
      {
        name: '财经',
        id: 'finance'
      },
      {
        name: '科技',
        id: 'keji'
      },
      {
        name: '教育',
        id: 'education'
      },
      {
        name: '汽车',
        id: 'car'
      }
    ],
    scrollInto: '',
    showTips: false,
    navigateFlag: false,
    pulling: false
  },

  onLoad: function (options) {
    setTimeout(() => {
      this.setData({
        newsList: this.randomfn()
      })
    }, 50);
  },
  randomfn() {
    let ary = [];
    for (let i = 0, length = this.data.tabBars.length; i < length; i++) {
      let aryItem = {
        loadingText: '正在加载...',
        refreshing: false,
        refreshText: '',
        data: [],
        isLoading: false,
        pageIndex: 1
      };
      if (i === 0) {
        aryItem.pageIndex = 2;
        aryItem.data = aryItem.data.concat(newsData);
      }
      ary.push(aryItem);
    }
    return ary;
  },
  getList(index, refresh) {
    let activeTab = this.data.newsList[index];
    let list = newsData || [];
    if (refresh) {
      activeTab.data = [];
      activeTab.loadingText = '正在加载...';
      activeTab.pageIndex = 2;
      activeTab.data = list || [];
    } else {
      activeTab.data = activeTab.data.concat(list);
      activeTab.pageIndex++;
      activeTab.isLoading = false;
      //根据实际修改判断条件
      if (activeTab.pageIndex > 3) {
        activeTab.loadingText = '没有更多了';
      }
    }
    this.setData({
      [`newsList[${index}]`]: activeTab
    })
  },
  goDetail(e) {
    if (this.data.navigateFlag) return;
    this.data.navigateFlag = true;
    wx.navigateTo({
      url: '/pages/template/news/newsDetail/newsDetail'
    });
    setTimeout(() => {
      this.data.navigateFlag = false;
    }, 200);
  },
  loadMore(e) {
    let index=this.data.tabIndex
    let activeTab = this.data.newsList[index];
    if (activeTab.pageIndex < 4 && !activeTab.isLoading) {
      let value = `newsList[${index}].isLoading`
      this.setData({
        [value]: true
      })
      setTimeout(() => {
        this.getList(index);
      }, 300);
    }
  },
  tabClick(e) {
    let index = e.target.dataset.current || e.currentTarget.dataset.current;
    this.switchTab(index);
  },
  tabChange(e) {
    if (e.detail.source == 'touch') {
      let index = e.target.current || e.detail.current;
      this.switchTab(index);
    }
  },
  switchTab(index) {
    if (this.data.tabIndex === index) return;
    if (this.data.newsList[index].data.length === 0) {
      this.getList(index);
    }
    // 缓存 tabId
    if (this.data.newsList[this.data.tabIndex].pageIndex > MAX_CACHE_PAGEINDEX) {
      let isExist = this.data.cacheTab.indexOf(this.data.tabIndex);
      if (isExist < 0) {
        this.data.cacheTab.push(this.data.tabIndex);
      }
    }
    let scrollIndex = index - 1 < 0 ? 0 : index - 1;
    this.setData({
      tabIndex: index,
      scrollInto: this.data.tabBars[scrollIndex].id
    })

    // 释放 tabId
    if (this.data.cacheTab.length > MAX_CACHE_PAGE) {
      let cacheIndex = this.data.cacheTab[0];
      this.clearTabData(cacheIndex);
      this.data.cacheTab.splice(0, 1);
    }
  },
  onrefresh(e) {
    let index=this.data.tabIndex;
    var tab = this.data.newsList[index];
    if (tab.refreshing) return;
    this.setData({
      [`newsList[${index}].refreshing`]: true,
      [`newsList[${index}].refreshText`]: '正在刷新...'
    })

    setTimeout(() => {
      this.getList(index, true);
      this.setData({
        pulling: true,
        [`newsList[${index}].refreshing`]: false,
        [`newsList[${index}].refreshText`]: '刷新成功',
        [`newsList[${index}].refreshFlag`]: false
      })
      wx.showToast({
        title: '刷新成功',
        icon: 'none'
      });
      setTimeout(() => {
        this.setData({
          pulling: false
        })
      }, 500);
    }, 1000);
  }
})